Dashboard Temp Share Shortlinks Frames API

HTMLify

81. Search in Rotated Sorted Array II.java
Views: 1 | Author: cody
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// 81. Search in Rotated Sorted Array II java solution
class Solution {
    public boolean search(int[] nums, int target) {
        int lo=0 , hi=nums.length-1;

        int ans=-1;
        while(lo<=hi){
            int m=lo+((hi-lo)/2);
            if(nums[m]==target){
                return true;
            }
            if(nums[m]==nums[lo] && nums[m]==nums[hi]){
                lo++;hi--;
            }else if(nums[m]<=nums[hi]){
                if(target>nums[m] && target<=nums[hi]){
                    lo=m+1;
                }else{
                    hi=m-1;
                }
            }else{
                if(target>=nums[lo] && target<nums[m]){
                    hi=m-1;
                }else{
                    lo=m+1;
                }
            }
        }
        return false;
    }
}